package com.adtech.adms.device.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.commons.web.action.DataGridAction;
import com.adtech.adms.device.pojo.GuiPolicy;
import com.adtech.adms.device.service.PolicyService;
import com.adtech.adms.system.pojo.Topo;
import com.adtech.adms.system.service.TopoService;
import com.adtech.adms.system.web.action.UserAction;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.exception.SystemException;

public class PolicyAction extends DataGridAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1345346L;
	private static final Logger log = Logger.getLogger(UserAction.class);
	private PolicyService policyService;
	private TopoService topoService;
	private GuiPolicy policy;
	private String id;
	private String topoNames;
	private List<TreeNode<Topo>> nodes;
	private boolean allLevel = true;
	
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	public String getTopoNames() {
		return topoNames;
	}

	public void setTopoNames(String topoNames) {
		this.topoNames = topoNames;
	}

	public TopoService getTopoService() {
		return topoService;
	}

	public void setTopoService(TopoService topoService) {
		this.topoService = topoService;
	}

	public List<TreeNode<Topo>> getNodes() {
		return nodes;
	}

	public void setNodes(List<TreeNode<Topo>> nodes) {
		this.nodes = nodes;
	}

	public PolicyService getPolicyService() {
		return policyService;
	}

	public void setPolicyService(PolicyService policyService) {
		this.policyService = policyService;
	}
	
	public GuiPolicy getPolicy() {
		return policy;
	}

	public void setPolicy(GuiPolicy guiPolicy) {
		this.policy = guiPolicy;
	}

	public String getDetail() {
		if (id != null) {
			policy = policyService.getPolicy(new Integer(id));
			if(policy != null){
				List<Topo> topos = policy.getTopos();
				topoNames = generateTopoNames(topos);
			}

		}
		return SUCCESS;
	}
	
	private String generateTopoNames(List<Topo> topos) {
		StringBuffer topoNames = new StringBuffer();
		for(Topo topo : topos){
			topoNames.append(topo.getName()+" ");
		}
		return topoNames.toString();
	}

	public String main(){
		return SUCCESS;
	}
	
	public String getTopoTree() {
		
	    nodes = topoService.getTopoTree(allLevel);
		
		return SUCCESS;
	}

	
	public String save() {
		policyService.save(policy);
		return SUCCESS;
	}
	public String delete() {
	
		policyService.delete(id);
		return SUCCESS;
	}
	

	@Override
	protected List<GuiPolicy> getModelDataGridList(Pager pager, FilterMap filtermap,
			Sort sort) throws SystemException, ServiceException {
	
		return policyService.getPolicyList(pager, filtermap.toExpression(), sort);
	}

}
